<?php

namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;

//配置
class Config extends BaseController
{
    protected $table = 'config';
    public function site()
    {
        if($this->request->isPost())
        {
            $p=input();
            foreach($p as $k=>$v)
            {
                Db::name('config')->where(['tag'=>$k])->update(['value'=>$v]);
            }
            redirect(url('site'))->send();
        }        
        $data = Db::name('config')->where(['type' => 'yongjin'])->select()->toArray();
        $config = array();
        foreach ($data as $k => $v) {
            //$config[$v['tag']] = $v['value'];
        }
        //return var_dump($config);
        View::assign('config', $data);
        return View::fetch();
    }
    public function percent()
    {
        if($this->request->isPost())
        {
            $p=input();
            foreach($p as $k=>$v)
            {
                Db::name('config')->where(['tag'=>$k])->update(['value'=>$v]);
            }
            redirect(url('percent'))->send();
        }        
        $data = Db::name('config')->where(['type' => 'percent'])->select()->toArray();
        $config = array();
        foreach ($data as $k => $v) {           
            $config[$v['tag']] = $v['value'];
        }
        View::assign('config', $config);
        return View::fetch();
    }
    public function index()
    {
        if($this->request->isPost())
        {
            $p=input();
            foreach($p as $k=>$v)
            {
                Db::name('config')->where(['tag'=>$k])->update(['value'=>$v]);
            }
            redirect(url('index'))->send();
        }        
        $data = Db::name('config')->where(['type' => 'site'])->select()->toArray();
        $config = array();
        foreach ($data as $k => $v) {           
            $config[$v['tag']] = $v['value'];
        }       
        View::assign('config', $config);
        return View::fetch();
    }
     public function weixin()
    {
        if($this->request->isPost())
        {
            $p=input();
            $wx = Db::name('weixin')->where(['id'=>1])->find();
            if($wx)
            {
                Db::name('weixin')->where(['id'=>1])->update($p);
            }
            else {
                Db::name('weixin')->insert($p);
            }
            redirect(url('weixin'))->send();
        }        
        $data = Db::name('weixin')->where(['id' => 1])->find();
         
        View::assign('config', $data);
        return View::fetch();
    }  
    public function order()
    {
        if($this->request->isPost())
        {
            $p=input();
            foreach($p as $k=>$v)
            {
                Db::name('config')->where(['tag'=>$k])->update(['value'=>$v]);
            }
            redirect(url('order'))->send();
        }        
        $data = Db::name('config')->where(['type' => 'order'])->select()->toArray();
        $config = array();
        foreach ($data as $k => $v) {           
            $config[$v['tag']] = $v['value'];
        }       
        View::assign('config', $config);
        return View::fetch();
    }
    
    public function road()
    {
        return View::fetch();
    } 
}
